package com.ycz;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.support.TransactionTemplate;


@Service
public class TestTransactionService1 {

    private TransactionTemplate transactionTemplate;

    // 通过构造器注入
    public TestTransactionService1(PlatformTransactionManager transactionManager) {

        this.transactionTemplate = new TransactionTemplate(transactionManager);

        // 可选：配置事务属性（默认继承自事务管理器）
        this.transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);

        this.transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

        this.transactionTemplate.setTimeout(30); // 单位：秒

    }

    public void test() {
        transactionTemplate.execute(status -> {
            return test2();
        });
    }

    public String test2() {
        return null;
    }

}
